package pers.cz.date;

import cn.hutool.core.date.format.FastDateFormat;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

/**
 * @program: PostGirl-panent
 * @description: DateUtils
 * @author: Cheng Zhi
 * @create: 2021-10-09 16:34
 **/
public class DateUtils {

    /**
     * UTC时间转换  yyyy-MM-dd'T'HH:mm:ss.SSSSSSZ eg 2016-10-26T08:20:53.131252Z:
     * @return Date
     * @throws ParseException
     * @author WANGZY25
     * */
    public static Date convertTimeFormat(String sourceTime) throws ParseException {

        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSSSSSZ");// 注意格式化的表达式
        sourceTime = sourceTime.replace("Z", " UTC");//注意是空格+UTC
        Date resDate = format.parse( sourceTime );
        return resDate;
    }

    /**
     * 获取当前时间的前day天
     * @param day
     * @return
     */
    public static Date getBeforeDate(Integer day) {

        Date date = new Date();
        Calendar now = Calendar.getInstance();
        now.setTime(date);
        now.add(Calendar.DAY_OF_MONTH, -day);

        return now.getTime();
    }

    /**
     * 获取当前时间的后day天
     * @param day
     * @return
     */
    public static Date getAfterDate(Integer day) {

        Date date = new Date();
        Calendar now = Calendar.getInstance();
        now.setTime(date);
        now.add(Calendar.DAY_OF_MONTH, day);

        return now.getTime();
    }

    /**
     * 格式化时间
     * @param d
     * @param format
     * @return
     */
    public static String format(java.util.Date d, String format) {
        return d == null ? "" : FastDateFormat.getInstance(format).format(d);
    }

    /**
     * 获取当前时间戳
     * @return
     */
    public static Long getCurrentTimestamp() {
        return new Date().getTime();
    }

    public static void main(String[] args) throws ParseException {

        System.out.println(getBeforeDate(-2));
        //DateUtils.format((Date)value, "yyyy-MM-dd HH:mm:ss")
        System.out.println(format(new Date(), "YYYY年MM月dd日"));
    }
}
